package cn.iocoder.yudao.module.aiposter.controller.app.submit;

import cn.iocoder.yudao.module.aiposter.service.submit.AppSubmitService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.aiposter.controller.app.submit.vo.*;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.submit.SubmitDO;

@Tag(name = "用户 APP - 提交海报信息")
@RestController
@RequestMapping("/ai-poster/submit")
@Validated
public class AppSubmitController {

    @Resource
    private AppSubmitService appSubmitService;

    @PostMapping("/create")
    @Operation(summary = "创建提交海报信息")
    public CommonResult<Long> createSubmit(@Valid @RequestBody AppSubmitSaveReqVO createReqVO) {
        return success(appSubmitService.createSubmit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提交海报信息")
    public CommonResult<Boolean> updateSubmit(@Valid @RequestBody AppSubmitSaveReqVO updateReqVO) {
        appSubmitService.updateSubmit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提交海报信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSubmit(@RequestParam("id") Long id) {
        appSubmitService.deleteSubmit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提交海报信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppSubmitRespVO> getSubmit(@RequestParam("id") Long id) {
        SubmitDO submit = appSubmitService.getSubmit(id);
        return success(BeanUtils.toBean(submit, AppSubmitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提交海报信息分页")
    public CommonResult<PageResult<AppSubmitRespVO>> getSubmitPage(@Valid AppSubmitPageReqVO pageReqVO) {
        PageResult<SubmitDO> pageResult = appSubmitService.getSubmitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSubmitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提交海报信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportSubmitExcel(@Valid AppSubmitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubmitDO> list = appSubmitService.getSubmitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "提交海报信息.xls", "数据", AppSubmitRespVO.class,
                        BeanUtils.toBean(list, AppSubmitRespVO.class));
    }

}